package com.maple.phoneward.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.maple.phoneward.constants.Constants;
import com.maple.phoneward.custom.DialogBuilder;
import com.maple.phoneward.custom.MainAdapter;
import com.maple.phoneward.utils.AppUtil;
import com.maple.phoneward.utils.Logger;
import com.maple.phoneward.utils.StringUtil;

/**
 * MainActivity.java
 * 
 * @author YaJian Qiu
 * @time 2013-3-17 下午10:21:47<br>
 *       本类描述：应用主界面
 * 
 */
public class MainActivity extends Activity implements OnItemClickListener {

	private static final String TAG = "MainActivity";
	private static final int REQUEST_CODE = 1;

	private ListView listView;
	private TextView tipTop, tipBottom;
	private Button openButton;
	private MainAdapter adapter;
	private boolean isServiceOpen = false;// 防盗服务是否开启

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		listView = (ListView) findViewById(R.id.main_list_view);
		tipTop = (TextView) findViewById(R.id.main_top_tips1);
		tipBottom = (TextView) findViewById(R.id.main_top_tips2);
		openButton = (Button) findViewById(R.id.main_open_button);
		findViewById(R.id.titile_back_button).setVisibility(View.GONE);
		adapter = new MainAdapter(this);
		listView.setAdapter(adapter);// 设置适配器
		listView.setOnItemClickListener(this);// 设置监听
		// Intent intent = new Intent(this, SystemService.class);
		// startService(intent);
		// CmdUtil cmdUtil = new CmdUtil(this);
		// System.out.println(cmdUtil.CMD_BACKUP_DATA);
		// IntentFilter filter = new
		// IntentFilter("android.provider.Telephony.SMS_RECEIVED");
		// filter.setPriority(Constants.INTEGER_MAX_VALUE);
		// AppBroadcastReceiver receiver = new AppBroadcastReceiver();
		// registerReceiver(receiver, filter);
		// SmsContentObserver observer = new SmsContentObserver(new Handler(),
		// this);
		// this.getContentResolver().registerContentObserver(SmsContentObserver.URI,
		// true, observer);
	}

	@Override
	protected void onResume() {
		super.onResume();
		// 根据防盗服务是否开启来显示不同的文字提示
		isServiceOpen = AppUtil.getFromSharedPreferences(this, Constants.IS_SERVICE_OPEN, Boolean.class);
		Logger.d(TAG, "openService防盗服务是否开启:" + isServiceOpen);
		showTextTip();
	}

	/**
	 * 根据防盗服务是否开启来显示不同的文字提示
	 */
	private void showTextTip() {
		if (isServiceOpen) {
			openButton.setText(R.string.stop_phoneward_button);
			tipTop.setText(R.string.phoneward_open_tip_top);
			tipTop.setTextColor(getResources().getColor(R.color.gred));
			tipBottom.setText(R.string.phoneward_open_tip_bottom);
		} else {
			openButton.setText(R.string.open_phoneward_button);
			tipTop.setText(R.string.phoneward_close_tip_top);
			tipTop.setTextColor(getResources().getColor(R.color.red));
			tipBottom.setText(R.string.phoneward_close_tip_bottom);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		String title = (String) adapter.getItem(position);
		Logger.d(TAG, "点击[" + title + "], 位置=[" + position + "]");
		Intent intent = new Intent(this, FunctionActivity.class);
		if (position == Constants.FUN_SYSTEM_SETTING) {
			intent = new Intent(this, SettingActivity.class);
		}
		// 启动功能界面
		intent.putExtra("title", title);
		intent.putExtra("position", position);
		startActivity(intent);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 在住界面监听返回键
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			new DialogBuilder(this)//
					.setMessage(R.string.quit_msg)//
					.setOnClickListener(new DialogInterface.OnClickListener() {//
								public void onClick(DialogInterface dialog, int position) {
									if (position == DialogBuilder.LEFT_BUTTON) {
										MainActivity.this.finish();
									}
								}
							}).create().show();
		}
		return super.onKeyDown(keyCode, event);
	}

	public void openService(View view) {
		isServiceOpen = AppUtil.getFromSharedPreferences(this, Constants.IS_SERVICE_OPEN, Boolean.class);
		Logger.d(TAG, "openService防盗服务是否开启:" + isServiceOpen);
		if (isServiceOpen) {
			// 如果服务已经开启则关闭服务
			new DialogBuilder(this)//
					.setMessage(R.string.phoneward_close_tip_message)//
					.setOnClickListener(new DialogInterface.OnClickListener() {//
								public void onClick(DialogInterface dialog, int position) {
									if (position == DialogBuilder.LEFT_BUTTON) {
										// 关闭防盗服务
										startOrStopService(false);
									}
								}
							}).create().show();
		} else {
			// 如果服务已经关闭则开启服务
			// 开启服务前判断用户是否设置了防盗密码、安全号码和备份邮箱
			String phonewardPwd = AppUtil.getFromSharedPreferences(this, Constants.CONFIG_PHONEWARD_PASSWORD, String.class);
			String safetyNumber1 = AppUtil.getFromSharedPreferences(this, Constants.CONFIG_SAFETY_PHONE_NUMBER_1, String.class);
			String safetyNumber2 = AppUtil.getFromSharedPreferences(this, Constants.CONFIG_SAFETY_PHONE_NUMBER_2, String.class);
			String email = AppUtil.getFromSharedPreferences(this, Constants.CONFIG_EMAIL, String.class);
			Intent intent = new Intent(this, FirstSetActivity.class);
			//startActivityForResult(intent, REQUEST_CODE);
			if (StringUtil.isEmpty(phonewardPwd) || (StringUtil.isEmpty(safetyNumber1)&&StringUtil.isEmpty(safetyNumber2)) || StringUtil.isEmpty(email)) {
				 startActivityForResult(intent, REQUEST_CODE);
			} else {
				// 开启防盗服务
				startOrStopService(true);
			}
		}

		// 开启防盗服务后保存用户设置
		// HashMap<String, String> params = new HashMap<String, String>();
		// params.put(Constants.CONFIG_PHONE_NUMBER,
		// AppUtil.getPhoneNumber(this));//本机号码
		// params.put(Constants.CONFIG_SIM_SEQUENCE,
		// AppUtil.getSimSerialNumber(this));//SIM卡序列号
		// params.put(Constants.CONFIG_SAFETY_PHONE_NUMBER,
		// "15555215556");//安全号码
		// params.put(Constants.CONFIG_EMAIL, "qiuyajian@qq.com");//安全号码
		// params.put(Constants.CONFIG_PHONE_IMEI, AppUtil.getDeviceId(this));
		// params.put(Constants.CONFIG_PHONE_TYPE, AppUtil.getPhoneType());
		// params.put(Constants.CONFIG_SYSTEM_VERSION,
		// AppUtil.getSystemVersion());
		// AppUtil.saveToSharedPreferences(this, "Integer", 1);
		// AppUtil.saveToSharedPreferences(this, "Float", 1f);
		// AppUtil.saveToSharedPreferences(this, "Long", 1l);
		// AppUtil.saveToSharedPreferences(this, "Boolean", true);
		// AppUtil.saveToSharedPreferences(this, "String", "test");
		// AppUtil.saveToSharedPreferences(this, params);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQUEST_CODE) {
			if (resultCode == RESULT_OK)
				startOrStopService(true);// 设置完成启动防盗服务
			else
				AppUtil.showLongMessage(this, AppUtil.getString(this, R.string.require_config));
		}
	}

	/**
	 * 开启或关闭防盗服务
	 * 
	 * @param flag
	 *            true开启 false关闭
	 */
	private void startOrStopService(boolean flag) {
		// if (flag) {
		// // 开启防盗服务
		// } else {
		// // 关闭防盗服务
		// }
		isServiceOpen = !isServiceOpen;// 切换服务的状态
		AppUtil.saveToSharedPreferences(this, Constants.IS_SERVICE_OPEN, isServiceOpen);
		showTextTip();// 显示提示
	}

}
